﻿using AiXiu.BLL;
using AiXiu.IBLL;
using AiXiu.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AiXiu.Web
{
    /// <summary>
    /// 视频上传处理程序
    /// </summary>
    public class VideoUploadHandler : IHttpHandler
    {
        /// <summary>
        /// 处理请求
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            // 读取参数
            string fileName = HttpUtility.UrlDecode(context.Request.QueryString["filename"]);
            string headline = HttpUtility.UrlDecode(context.Request.QueryString["headline"]);
            //视频名称未填写默认短视频
            if (string.IsNullOrWhiteSpace(headline))
            {
                headline = "短视频";
            }
            string location = HttpUtility.UrlDecode(context.Request.QueryString["location"]);
            // 从用户缓存信息读取当前登录用户Id
            TBUsers user = IdentityManager.ReadUser();
            int userId = user.Id;
            // 调用方法，获取视频上传凭证
            IUserManager userManager = new UserManager();
            OperResult<CreateUploadVideoResult> createResult = userManager.ToBeUpload(userId, fileName, headline, location);
            // 输出
            string jsonString = JsonConvert.SerializeObject(createResult.ReadResult());
            context.Response.ContentType = "text/plain";
            context.Response.Write(jsonString);
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}